package com.ruoyi.onlinevideo.config;/**
 * @InterfaceName: WebSocketConfig
 * @Description:
 * @Author: lilirui
 * @Date: 2022-08-30
 */

import com.ruoyi.onlinevideo.handler.MyWebSocketHandler;
import com.ruoyi.onlinevideo.intercepter.MyHandShakeInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 *@InterfaceName: WebSocketConfig
 *@Description: 声明socket配置类
 *@Author: lilirui
 *@Date: 2022-08-30
 */
@Component
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myHandler(), "/ws")
                .addInterceptors(new MyHandShakeInterceptor()).setAllowedOrigins("*");
    }
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
    @Bean
    public MyWebSocketHandler myHandler() {
        return new MyWebSocketHandler();
    }
}